#!/usr/local/bin/python
# -*- encoding:utf-8 -*-

import numpy as np;
import scipy.sparse as sp
from scipy import linalg
import time
import threading
import os
from scipy.sparse.linalg import eigsh
from multiprocessing import Process, Manager
import sys
import utils
from collections import OrderedDict;

'''This script is designed to compare bm25 result, rmls result with labeled results'''
class Record():
    def __init__(self, res, appTitle):
        self.labeled_res = res;
        self.appTitle = appTitle;
        self.bm25res = None;
        self.rmlsres = None;
        self.recall = False;

labeled = open('../test_collections', 'r');
K = 603;
labeled_di = {};
for i in range(K):
    labeled_di[str(i+1)] = OrderedDict();

queryDi = {};
for line in labeled:
    li = line.split('\t');
    queryID = li[0];
    queryString = li[1];
    appID = li[2];
    res = float(li[3]);
    appTitle = li[4].strip('\n');
    labeled_di[queryID][appID] = Record(res, appTitle); 
    queryDi[queryID] = queryString;
labeled.close();


results = open('results', 'r');
for line in results:
    li = line.strip().split('\t');
    queryID = li[0];
    appID = li[2];
    if appID in labeled_di[queryID]:
        labeled_di[queryID][appID].recall = True;

results.close();

bm25result = open('bm25_results', 'r');
for line in bm25result:
    li = line.strip().split('\t');
    queryID = li[0];
    appID = li[3];
    res = float(li[1])
    labeled_di[queryID][appID].bm25res = res;

bm25result.close();


rmlsresult = open('rmls_final_results', 'r');

for line in rmlsresult:
    li = line.strip().split('\t');
    queryID = li[0];
    appID = li[3];
    res = float(li[1]);
    labeled_di[queryID][appID].rmlsres = res;
rmlsresult.close();

#For Fancy print out
def queryHasRmls(value):
	flag = False;
	for v in value.values():
		if v.rmlsres != None:
			flag = True;
			break;
	return flag;
for i in range(K):
    queryID = str(i+1);
    rmlsFlag = queryHasRmls(labeled_di[queryID]);
    #if not rmlsFlag:
    print('----------------------------------------Here %s : %s results----------------' % (queryID, queryDi[queryID]));
    for key, value in labeled_di[queryID].items():
        #if not rmlsFlag:
        print '\t\t%-10s\t%-5s\t%-10s\t%-10s\t%-10s\t%-60s' % (key, str(value.labeled_res), value.recall and '*', value.bm25res or 'NB', value.rmlsres or'NR', value.appTitle); 





